<?php
namespace Home\Controller;
use Think\Controller;
class ClubVideoController extends Controller {
    function  upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Upload/'; // 设置附件上传根目录
        //// 上传单个文件
        $info = $upload->uploadOne($_FILES['pic']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }

        return $info;
    }
    public function index(){
        $video = D('video');
        $list = $video->select();
        $this->assign(array(
            'list'        => $list
        ));
        $this->display();
    }

    function del(){
        $video = D('video');
        $id = I('get.id');
        $de = $video->where("id=$id")->delete();
        // $json = array('success' =>$de);
        // echo json_encode($json);
        if($de==1){
            $this -> success('删完啦');
        }else{
            $this -> error('出错了');
        }
    }

    function xq(){
        $video = D('video');
        $id = I('get.id');
        $info = $video -> where(['id'=>$id])->find();
        $this->assign('data',$info);
        $this->display();
    }

    function edit(){
        //  $id=I('get.id');
        $video = D('video');
        $id = I('get.id');
        $info = $video -> where(['id'=>$id])->find();
        $this->assign('data',$info);
        $this->display();
    }
    function add(){
        $this->display();
    }
    function doAdd(){
        $info=$this->upload();
        $video=D('video');
        $data['img']=$info['savepath'].$info['savename'];
        if($vo = $video->create()) {
            $video->img=$data['img'];
            $result = $video->add();
            if($result !== false){
                $this -> success('数据添加成功！');
            }else{
                $this -> error('数据添加失败！');
            }
        }else{
            $this->error($video->getError());
        }
    }
    function doEdit(){
        $info=$this->upload();
        $video=D('video');
        $data['img']=$info['savepath'].$info['savename'];
        if($vo = $video->create()) {
            $video->img=$data['img'];
            $result = $video->save();
            if($result !== false){
                $this ->  success('数据更新成功！');
            }else{
                $this -> error('数据更新失败');
            }
        }else{
            $this->error($video->getError());
        }
    }
}